// pages/discount/discount.js
import {
    requestUtil,
    getbaseUrl
} from "../../utils/request"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        discountData: [],
        disActiveDate: [],
        undisActiveDate: []
    },

    // 过滤数据
    filterData(data) {

        if(data.length==0){
            this.setData({
                disActiveDate: [],
                undisActiveDate: []
            })
            return
        }

        let newData = data.map(item => {
            let startyear = item.startDate.split(" ")[0].split("-")
            let endtyear = item.dealDate.split(" ")[0].split("-")
            item.startDate =startyear[0] + "." +startyear[1] + "." +startyear[2]
            item.dealDate =endtyear[0] + "." +endtyear[1] + "." +endtyear[2]
        //    console.log(item)
           return item
        })

       
        let arr1 = newData.filter(item => {

            return item.disActive == 2
        })

        // 1是过期的
        let arr2 = newData.filter(item => {
            return item.disActive == 1
        })

        this.setData({
            disActiveDate: arr1,
            undisActiveDate: arr2
        })
    },

    // 获取用户的优惠券

    getuserDiscount() {
       
        let openId = this.data.userInfo.openid
        // console.log(openId)
        let res = requestUtil({
            url: "/shop/getUserAddDiscount",
            method: "get",
            data: {
                openId
            }
        })
        res.then(({
            data
        }) => {
            // console.log(data)
            if (data.status == 200) {
                // 对比数据渲染
                this.filterData(data.data)
                this.setData({
                    discountData: data.data
                })
                wx.hideLoading()
            } else {
                this.filterData([])
                this.setData({
                    discountData: []
                })
                wx.hideLoading()
            }
        })
    },

    // 获取用户的信息
    getuserInfo() {
        wx.getStorage({
            key: "userInfo",
            success: (res) => {
                let userInfo = JSON.parse(res.data)
                // console.log(userInfo)
                this.setData({
                    userInfo
                })

            },
            fail: (res) => {
                this.setData({
                    userInfo: {}
                })
            }
        })
    },

    // 使用优惠券
    tostore(){
        wx.switchTab({
          url: '/pages/address/address',
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getuserInfo()
        setTimeout(() => {
            this.getuserDiscount()
        }, 100)
        wx.showLoading({
            title: '数据加载中',
          })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})